package com.spring.server;

import com.spring.handler.RequestProcessor;
import com.spring.handler.ResourceLoader;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

/**
 * @author Spring King
 * @date 2021/7/10
 */
public class Bootstrap {

    public void start() throws Exception {
        // 加载 web.xml
        KingService service = ResourceLoader.loadServlet();
        ServerSocket serverSocket = new ServerSocket(service.getPort());
        // v1.0 基础访问
//        serverV1(serverSocket);
        // v2.0 封装request和response对象，返回HTML静态资源
//        serverV2(serverSocket);
        // v3.0 请求动态资源(Servlet)
//        serverV3(serverSocket);
        // v4.0 多线程-Thread
//        serverV4(serverSocket);

        // v4.1 多线程-pool
        // 定义一个线程池
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        while (true) {
            Socket socket = serverSocket.accept();
            RequestProcessor requestProcessor = new RequestProcessor(socket, service);
            threadPoolExecutor.execute(requestProcessor);
        }
    }

    private ThreadPoolExecutor getThreadPoolExecutor() {
        int corePoolSize = 10;
        int maximumPoolSize = 50;
        long keepAliveTime = 100L;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(50);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

/*
    private void serverV1(ServerSocket serverSocket) throws IOException {
        Socket socket = serverSocket.accept();
        OutputStream outputStream = socket.getOutputStream();
        String msg = "Hello KingCat";
        String responseText = HttpProtocolUtil.getHttpHeader200(msg.getBytes().length) + msg;

        outputStream.write(responseText.getBytes(StandardCharsets.UTF_8));
        socket.close();
    }

    private void serverV2(ServerSocket serverSocket) throws IOException {
        while (true) {
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            response.outputHtml(request.getUrl());
            socket.close();
        }
    }

    private void serverV3(ServerSocket serverSocket) throws Exception {
        while (true) {
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            HttpServlet httpServlet = servletMap.get(request.getUrl());
            if (httpServlet == null) {
                response.outputHtml(request.getUrl());
            } else {
                httpServlet.service(request, response);
            }
            socket.close();
        }
    }

    private void serverV4(ServerSocket serverSocket) throws IOException {
        while (true) {
            Socket socket = serverSocket.accept();
            RequestProcessor requestProcessor = new RequestProcessor(socket, servletMap);
            requestProcessor.start();
        }
    }
*/
}
